﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace PlanningGIS.Util
{
    /// <summary>
    /// 对象扩展
    /// </summary>
    public static class ObjectExtension
    {
        /// <summary>
        /// 复制对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static object Clone(this object obj)
        {
            BinaryFormatter bFormatter = new BinaryFormatter();
            MemoryStream stream = new MemoryStream();
            bFormatter.Serialize(stream, obj);
            stream.Seek(0, SeekOrigin.Begin);
            return bFormatter.Deserialize(stream);
        }

        /// <summary>
        /// 去处字符串中指定的字符
        /// </summary>
        /// <param name="text">待处理的字符串</param>
        /// <param name="value">要移除的值</param>
        /// <returns></returns>
        public static string RemoveChar(this string text, char value)
        {
            if (string.IsNullOrWhiteSpace(text)) return text;
            string rst = "";
            foreach (char item in text)
            {
                if (item != value)
                    rst = rst + item;
            }
            return rst;
        }

        /// <summary>
        /// 将全角字符串转半角字符串
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns></returns>
        public static string ToDBC(this string input)
        {
            //全角空格为12288，半角空格为32,其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char)(c[i] - 65248);
            }
            return new string(c);
        }

        /// <summary>
        /// 将半角字符串转全角字符串
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ToSBC(this string input)
        {
            //全角空格为12288，半角空格为32,其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 32)
                {
                    c[i] = (char)12288;
                    continue;
                }
                if (c[i] < 127)
                    c[i] = (char)(c[i] + 65248);
            }
            return new string(c);
        }

    }

    /// <summary>
    /// 枚举对象扩展
    /// </summary>
    public static class EnumExtension
    {
        /// <summary> 
        /// 获取枚举变量值的 Description 属性 
        /// </summary> 
        /// <param name="obj">枚举变量</param> 
        /// <returns>如果包含 Description 属性，则返回 Description 属性的值，否则返回枚举变量值的名称</returns> 
        public static string GetDescription(this object obj)
        {
            return GetDescription(obj, false);
        }

        /// <summary> 
        /// 获取枚举变量值的 Description 属性 
        /// </summary> 
        /// <param name="obj">枚举变量</param> 
        /// <param name="isTop">是否改变为返回该类、枚举类型的头 Description 属性，而不是当前的属性或枚举变量值的 Description 属性</param> 
        /// <returns>如果包含 Description 属性，则返回 Description 属性的值，否则返回枚举变量值的名称</returns> 
        public static string GetDescription(this object obj, bool isTop)
        {
            if (obj == null)
            {
                return string.Empty;
            }
            try
            {
                Type _enumType = obj.GetType();
                DescriptionAttribute dna = null;
                if (isTop)
                {
                    dna = (DescriptionAttribute)Attribute.GetCustomAttribute(_enumType, typeof(DescriptionAttribute));
                }
                else
                {
                    FieldInfo fi = _enumType.GetField(Enum.GetName(_enumType, obj));
                    dna = (DescriptionAttribute)Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute));
                }
                if (dna != null && string.IsNullOrEmpty(dna.Description) == false)
                    return dna.Description;
            }
            catch
            {

            }
            return obj.ToString();
        }

    }

}
